<!--
 * @Author: Liang 1931226988@qq.com
 * @Date: 2023-08-19 08:52:04
 * @LastEditors: Liang 1931226988@qq.com
 * @LastEditTime: 2023-08-21 15:04:34
 * @FilePath: \WanYi\src\pages\classification\classification.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="classification">
    <v-advertisement></v-advertisement>
    <div class="header">
      <v-advertisement></v-advertisement>
      <div class="search">       
        <image
          class="img"
          src="@/static/image/搜索.png"
          mode="widthFix"
        />
        <p class="test">123</p>
      </div>
    </div>
    <div class="center">
      <div class="left">
        <scroll-view
          class="menu"
          scroll-y
          enable-flex
          enhanced
          :show-scrollbar="false"
        >
        <div
            :class="{ menu_item: true, active: menuIndex == -1 }"
            
            @click="changeIndex(-1)"
          >
            为你推荐
          </div>
          <div
            :class="{ menu_item: true, active: menuIndex == index }"
            v-for="(item, index) in pageInfo.global?.cateList"
            :key="index"
            @click="changeIndex(index , item.id)"
          >
            {{ item.name }}
          </div>
        </scroll-view>
      </div>
      <scroll-view scroll-y :show-scrollbar="false" enhanced class="right">
        <div class="top">
          <image
            class="top_img"
            :src="pageInfo.data?.currentCategory.bannerList[0].picUrl"
          />
        </div>

        <!-- 为你推荐渲染 -->
        <div class="recommend" v-if="categoryId == 11">
          <div class="recommend_item" v-for="(item, index) in pageInfo.data?.categoryL2List" :key="item.id">
            <image
              class="recommend_item_img"
              :src="item.bannerUrl"
              mode="widthFix"
            />
            <div class="recommend_item_text">{{ item.name }}</div>
          </div>
        </div>

        <!-- 其他渲染 -->
        <div class="show" v-else v-for="(show , index) in pageInfo.data?.categoryGroupList" :key="show.id">
          <div class="show_title">{{ show.name }}</div>
          <div class="show_wrap">
            <div class="show_wrap_item" v-for="(item, index) in show.categoryList" :key="item.id">
              <image
                class="show_wrap_item_img"
                :src="item.wapBannerUrl"
                mode="widthFix"
              />
              <div class="show_wrap_item_text">{{ item.name }}</div>
            </div>
          </div>
        </div>
      </scroll-view>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted , watch } from "vue";
import { ref } from "vue";
import Ajax from '@/api/index'
import {onShow, onHide} from '@dcloudio/uni-app'

// 菜单选项控制
const menuIndex = ref<number>(-1);
// 页面id控制默认为11
const categoryId = ref<number>(11)
// 页面数据
const pageInfo = ref<any>({})

// 回调函数区域
// 切换菜单选中项
const changeIndex = (index: number , id:number = 0) => {
  menuIndex.value = index
  // 如果是为你推荐就格式化参数为11
  if(menuIndex.value == -1) {
    categoryId.value = 11
  }else {
    // 如果不是 就修改为相应参数
    categoryId.value = id
  }
};

// 获取页面数据
const getData = async () => {
  const time = Date.now()
  const res = await Ajax.reqClassificationInfo(time , categoryId.value)
  pageInfo.value = res
}

// 组合式api区域
onMounted(() => {
  getData()
})

onShow(() => {
  // 在每次展示页面都初始化页面
  menuIndex.value = -1
  categoryId.value = 11
  getData()
})


// 侦听器 监听页面id的变化从新获取数据
watch(categoryId , () => {
  // 从新获取数据
  getData()
})
</script>

<style scoped lang="less">
.classification {
  .header {
    box-sizing: border-box;
    position: fixed;
    top: 0;
    width: 100%;
    // padding: 10rpx 20rpx;
    border-bottom: 2rpx solid #ccc;
    background-color: #fff;
    z-index: 999;
    .search {
      margin: 10rpx 20rpx;
      height: 60rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      background-color: #ededed;
      border-radius: 10rpx;
      .img {
        width: 36rpx;
      }
      .test {
        color: #999;
      }
    }
  }
  .center {
    height: calc(100vh - 82rpx);
    margin-top: 80rpx;
    display: flex;
    .left {
      height: 100%;
      flex: 3;
      border-right: 2rpx solid #ccc;
      .menu {
        display: flex;
        flex-direction: column;
        height: 100%;
        .menu_item {
          text-align: center;
          padding: 30rpx 0;
          border-left: 6rpx solid transparent;
        }
        .active {
          border-left: 6rpx solid brown;
        }
      }
    }
    .right {
      padding: 30rpx;
      flex: 8;
      .top {
        margin-bottom: 20rpx;
        .top_img {
          width: 100%;
          height: 226rpx;
        }
      }
      .recommend {
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
        .recommend_item {
          margin-bottom: 30rpx;
          display: flex;
          flex-direction: column;
          align-items: center;
          width: 30%;
          .recommend_item_img {
            width: 100%;
          }
          .recommend_item_text {
            font-size: 28rpx;
          }
        }
      }

      .show {
        margin-bottom: 60rpx;
        .show_title {
          font-weight: bold;
          margin-bottom: 10rpx;
        }
        .show_wrap {
          border-top: 2rpx solid #ccc;
          display: flex;
          flex-wrap: wrap;
          .show_wrap_item {
            margin-top: 20rpx;
            margin-right: 3.33%;
            display: flex;
            flex-direction: column;
            align-items: center;
            width: 30%;
            .show_wrap_item_img {
              width: 100%;
            }
            .show_wrap_item_text {
              font-size: 28rpx;
            }
          }
        }
      }
    }
  }
}
</style>
